home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / NvidiaDetector / nvidiadetector.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  9.0 KB  |  317 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. import subprocess
  7. from subprocess import Popen, PIPE
  8. import sys
  9. import logging
  10. aliasesPath = '/usr/share/jockey/modaliases/'
  11.  
  12. class NoDatadirError(Exception):
  13.     '''Exception thrown when no modaliases dir can be found'''
  14.     pass
  15.  
  16.  
  17. class NvidiaDetection(object):
  18.     '''
  19.     A simple class to:
  20.       * Detect the available graphics cards
  21.       * See what drivers support them (If they are 
  22.         NVIDIA cards). If more than one card is 
  23.         available, try to find the highest common 
  24.         driver version which supports them all.
  25.         (READ the comments in the code for further
  26.         details)
  27.       * Return the recommended driver version
  28.     '''
  29.     oldPackages = [
  30.         'nvidia-glx',
  31.         'nvidia-glx-new',
  32.         'nvidia-glx-legacy',
  33.         'nvidia-glx-envy',
  34.         'nvidia-glx-new-envy',
  35.         'nvidia-glx-legacy-envy',
  36.         'nvidia-glx-177']
  37.     
  38.     def __init__(self, printonly = None, verbose = None, datadir = aliasesPath):
  39.         """
  40.         printonly = if set to None will make an instance
  41.                     of this class return the selected
  42.                     driver.
  43.                     If set to True it won't return
  44.                     anything. It will simply and print
  45.                     the choice.
  46.                     
  47.         verbose   = if set to True will make the methods
  48.                     print what is happening.
  49.         """
  50.         self.printonly = printonly
  51.         self.verbose = verbose
  52.         if not os.path.isdir(datadir):
  53.             print 'none'
  54.             logging.debug('dir %s not found' % datadir)
  55.             raise NoDatadirError, "datadir '%s' not found" % datadir
  56.         os.path.isdir(datadir)
  57.         self.datadir = datadir
  58.         self.detection()
  59.         self.getData()
  60.         self.getCards()
  61.         self.removeUnsupported()
  62.         if printonly == True:
  63.             self.printSelection()
  64.         else:
  65.             self.selectDriver()
  66.  
  67.     
  68.     def detection(self):
  69.         '''
  70.         Detect the models of the graphics cards
  71.         and store them in self.cards
  72.         '''
  73.         self.cards = []
  74.         p1 = Popen([
  75.             'lspci',
  76.             '-n'], stdout = PIPE)
  77.         p = p1.communicate()[0].split('\n')
  78.         indentifier1 = re.compile('.*0300: *(.+):(.+) \\(.+\\)')
  79.         indentifier2 = re.compile('.*0300: *(.+):(.+)')
  80.         for line in p:
  81.             m1 = indentifier1.match(line)
  82.             m2 = indentifier2.match(line)
  83.             if m1:
  84.                 id1 = m1.group(1).strip().lower()
  85.                 id2 = m1.group(2).strip().lower()
  86.                 id = id1 + ':' + id2
  87.                 self.cards.append(id)
  88.                 continue
  89.             if m2:
  90.                 id1 = m2.group(1).strip().lower()
  91.                 id2 = m2.group(2).strip().lower()
  92.                 id = id1 + ':' + id2
  93.                 self.cards.append(id)
  94.                 continue
  95.         
  96.  
  97.     
  98.     def getData(self):
  99.         '''
  100.         Get the data from the modaliases for each driver
  101.         and store them in self.drivers
  102.         '''
  103.         files = os.listdir(self.datadir)
  104.         self.drivers = { }
  105.         for file in files:
  106.             a = open(self.datadir + file, 'r')
  107.             lines = a.readlines()
  108.             indentifier = re.compile('.*alias pci:v0000(.+)d0000(.+)sv.*nvidia.*nvidia-glx-(.+).*')
  109.             for line in lines:
  110.                 m1 = indentifier.match(line)
  111.                 if m1:
  112.                     id1 = m1.group(1).strip().lower()
  113.                     id2 = m1.group(2).strip().lower()
  114.                     drivername = m1.group(3).strip().lower()
  115.                     fullname = 'nvidia-glx-%s' % drivername.strip()
  116.                     if id1 in ('10de', '12d2') and fullname not in self.oldPackages:
  117.                         self.drivers.setdefault(int(drivername), []).append(id1 + ':' + id2)
  118.                     
  119.                 fullname not in self.oldPackages
  120.             
  121.             a.close()
  122.         
  123.         if len(self.drivers.keys()) == 0:
  124.             print 'none'
  125.         
  126.  
  127.     
  128.     def getCards(self):
  129.         '''
  130.         See if the detected graphics cards are NVIDIA cards.
  131.         If they are NVIDIA cards, append them to self.nvidiaCards
  132.         '''
  133.         self.driversForCards = { }
  134.         self.nvidiaCards = []
  135.         for card in self.cards:
  136.             if card[0:card.find(':')] == '10de':
  137.                 if self.verbose:
  138.                     print 'NVIDIA card found (' + card + ')'
  139.                 
  140.                 self.nvidiaCards.append(card)
  141.                 continue
  142.         
  143.         self.orderedList = self.drivers.keys()
  144.         self.orderedList.sort(reverse = True)
  145.         for card in self.nvidiaCards:
  146.             supported = False
  147.             for driver in self.orderedList:
  148.                 if card in self.drivers[driver]:
  149.                     supported = True
  150.                     if self.verbose:
  151.                         print 'Card', card, 'supported by driver', driver
  152.                     
  153.                     self.driversForCards.setdefault(card, []).append(driver)
  154.                     continue
  155.             
  156.             if supported == False:
  157.                 self.driversForCards.setdefault(card, []).append(None)
  158.                 continue
  159.         
  160.  
  161.     
  162.     def removeUnsupported(self):
  163.         '''
  164.         Remove unsupported cards from self.nvidiaCards and from
  165.         self.driversForCards
  166.         '''
  167.         unsupportedCards = []
  168.         for card in self.driversForCards:
  169.             if None in self.driversForCards[card]:
  170.                 unsupportedCards.append(card)
  171.                 continue
  172.         
  173.         for unsupported in unsupportedCards:
  174.             if self.verbose:
  175.                 print 'Removing unsupported card', unsupported
  176.             
  177.             self.nvidiaCards.remove(unsupported)
  178.             del self.driversForCards[unsupported]
  179.         
  180.  
  181.     
  182.     def selectDriver(self):
  183.         '''
  184.         If more than one card is available, try to get the highest common driver
  185.         '''
  186.         cardsNumber = len(self.nvidiaCards)
  187.         if cardsNumber > 0:
  188.             if cardsNumber > 1:
  189.                 occurrence = { }
  190.                 for card in self.driversForCards:
  191.                     for drv in self.driversForCards[card]:
  192.                         occurrence.setdefault(drv, 0)
  193.                         occurrence[drv] += 1
  194.                     
  195.                 
  196.                 occurrences = occurrence.keys()
  197.                 occurrences.sort(reverse = True)
  198.                 candidates = []
  199.                 for driver in occurrences:
  200.                     if occurrence[driver] == cardsNumber:
  201.                         candidates.append(driver)
  202.                         continue
  203.                 
  204.                 if len(candidates) > 0:
  205.                     candidates.sort(reverse = True)
  206.                     choice = candidates[0]
  207.                     if self.verbose and not (self.printonly):
  208.                         print 'Recommended NVIDIA driver:', choice
  209.                     
  210.                 else:
  211.                     choice = occurrences[0]
  212.                     if self.verbose and not (self.printonly):
  213.                         print 'Recommended NVIDIA driver:', choice
  214.                     
  215.             else:
  216.                 choice = self.driversForCards[self.driversForCards.keys()[0]][0]
  217.                 if self.verbose and not (self.printonly):
  218.                     print 'Recommended NVIDIA driver:', choice
  219.                 
  220.             choice = 'nvidia-glx-' + str(choice)
  221.         elif self.verbose:
  222.             print 'No NVIDIA package to install'
  223.         
  224.         choice = None
  225.         return choice
  226.  
  227.     
  228.     def checkpkg(self, pkglist):
  229.         '''
  230.         USAGE:
  231.             * pkglist is the list of packages  you want to check
  232.             * use lists for one or more packages
  233.             * use a string if it is only one package
  234.             * lists will work well in both cases
  235.         '''
  236.         lines = []
  237.         notinstalled = []
  238.         p1 = Popen([
  239.             'dpkg',
  240.             '--get-selections'], stdout = PIPE)
  241.         p = p1.communicate()[0]
  242.         c = p.split('\n')
  243.         for line in c:
  244.             if line.find('\tinstall') != -1:
  245.                 lines.append(line.split('\t')[0])
  246.                 continue
  247.         
  248.         if self.isstr(pkglist) == True:
  249.             
  250.             try:
  251.                 if lines.index(pkglist):
  252.                     pass
  253.             except ValueError:
  254.                 notinstalled.append(pkglist)
  255.             except:
  256.                 None<EXCEPTION MATCH>ValueError
  257.             
  258.  
  259.         None<EXCEPTION MATCH>ValueError
  260.         for pkg in pkglist:
  261.             
  262.             try:
  263.                 if lines.index(pkg):
  264.                     pass
  265.             continue
  266.             except ValueError:
  267.                 notinstalled.append(pkg)
  268.                 continue
  269.             
  270.  
  271.         
  272.         return notinstalled
  273.  
  274.     
  275.     def isstr(self, elem):
  276.         if not isinstance(elem, type('')):
  277.             pass
  278.         return isinstance(elem, type(u''))
  279.  
  280.     
  281.     def islst(self, elem):
  282.         if not isinstance(elem, type(())):
  283.             pass
  284.         return isinstance(elem, type([]))
  285.  
  286.     
  287.     def getDrivers(self):
  288.         '''
  289.         oldPackages = a list of the names of the obsolete drivers
  290.         notInstalled = a list of the obsolete drivers which are not
  291.                        installed
  292.         '''
  293.         installedPackage = None
  294.         notInstalled = self.checkpkg(self.oldPackages)
  295.         for package in self.oldPackages:
  296.             if package not in notInstalled:
  297.                 installedPackage = package
  298.                 continue
  299.         
  300.         return len(notInstalled) != len(self.oldPackages)
  301.  
  302.     
  303.     def printSelection(self):
  304.         '''
  305.         Part for the kernel postinst.d/ hook
  306.         '''
  307.         driver = self.selectDriver()
  308.         if self.getDrivers():
  309.             if driver:
  310.                 print driver
  311.             else:
  312.                 print 'none'
  313.         else:
  314.             print 'none'
  315.  
  316.  
  317.